import NameMap from '@/api/country/BaseData/NameMap';
import CacheKeys from "@/constants/CacheKeys";
import {useCache} from "@/hooks/web/useCache";
import {CountryVO} from "@/types/country";

const {wsCache} = useCache();
const Countries:CountryVO[] = wsCache.get(CacheKeys.COUNTRY);
/**
 * 获取所有的城市数据
 */
export const getAllCountryData: CountryVO[] = () => {
    return Countries;
};

export const getCountryData: CountryVO[] = (year: number, keyName: string, trueValue: any) => {
    return Countries.filter((item) => item.year === unref(year)).filter((item) => item[keyName] === trueValue);
};


interface Cpi {
    xYear: number[];
    yCpi: number[];
}

/**
 * 获取指定国家通货膨胀的数据
 */
export const getCipData = (countryName: string): Cpi => {
    Countries.sort((a, b) => a.year - b.year);
    const xYear: number[] = [];
    const yCpi: number[] = [];
    Countries.forEach(item => {
        if (item.country === countryName) {
            xYear.push(item.year);
            yCpi.push(item.inflationAnnualCpi);
        }
    });
    return {
        xYear,
        yCpi,
    };
};

/**
 * 获取指定国家
 * @param countryName
 */
export const getUsdData = (countryName: string) => {
    Countries.sort((a, b) => a.year - b.year);
    const xYear: number[] = [];
    const yUsd: number[] = [];
    Countries.forEach(item => {
        if (item.country === countryName) {
            xYear.push(item.year);
            yUsd.push(item.exchUsd);
        }
    });
    return {
        xYear,
        yUsd,
    };
};

export const getCountData = (keyName: string, trueValue: any) => {
    // 按照国家分组
    // 每一组内统计出现的次数
    const temp = {};
    for (const item of Countries) {
        if (!temp[NameMap[item.country]]) {
            temp[NameMap[item.country]] = {count: 0};
        }
        if (item[keyName] === trueValue) {
            temp[NameMap[item.country]].count += 1;
        }
    }
    return temp;
};


